package cn.bobohost.market.web.controller;

import cn.bobohost.market.common.pojo.ResultDto;
import cn.bobohost.market.common.pojo.ResultPageDto;
import cn.bobohost.market.pojo.Employee;
import cn.bobohost.market.service.EmployeeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 员工操作的Controller
 */
//允许跨域
@CrossOrigin
@RestController
@RequestMapping("/emp")
@Tag(name = "员工管理")
public class EmployeeController {

    //注入service
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private HttpServletRequest request;
    /**
     * 查询员工列表
     * 注意：请求地址和方法必须符合接口文档；参数得能装进去
     * @param searchData
     * @return
     */
    @PostMapping("/listpage/{currentPage}/{pageSize}")
    @Operation(summary = "分页条件查询员工列表")
    @Parameters({
            @Parameter(name = "currentPage",description = "当前页码",required = true,in = ParameterIn.PATH),
            @Parameter(name = "pageSize",description = "每页最大记录数",required = true,in = ParameterIn.PATH)

    })
    public ResultDto getEmp( @RequestBody Employee searchData, @PathVariable int currentPage, @PathVariable int pageSize){
        String account = request.getHeader("Authorization");
        System.out.println("-----------------"+account);

        //调用业务层查询分页数据
        Page page = employeeService.findEmployeeListPage(searchData, currentPage, pageSize);
        //分页的结果重新封装
        ResultPageDto resultPageDto = ResultPageDto.of(page.getTotal(), page.getRecords());

        return ResultDto.success("查询分页列表成功！",resultPageDto);

    }

    @PostMapping
    @Operation(summary = "添加一个员工")
    public ResultDto addEmp(@RequestBody Employee employee){
        if(StringUtils.hasText(employee.getId())){
            //更新操作
            employeeService.updateEmployee(employee);
            return  ResultDto.success("修改员工成功！",null);
        }else{
            //保存操作
            employeeService.saveEmployee(employee);
            return  ResultDto.success("保存员工成功！",null);
        }
    }


    @DeleteMapping("/{id}")
    @Operation(summary = "删除员工")
    public ResultDto deleteEmpById(@PathVariable String id){

        employeeService.deleteEmployeeById(id);
        return  ResultDto.success("删除员工成功！",null);
    }

    @PostMapping("/login")
    @Operation(summary = "员工登录")
    public ResultDto login(@RequestBody Employee employee){
        Employee employeeLogin=employeeService.findEmpolyeeByAccountAndPassword(employee.getAccount(),employee.getPassword());

        //逻辑判断
        if(employeeLogin==null){
            //登录失败
            return ResultDto.success("登录失败，用户名或密码不正确",null);
        }

        //返回结果
        return ResultDto.success("登录成功",employeeLogin);


        //登录成功
//        //签发token
//        String token= JwtUtil.generateJwtTokenByRSA(employeeLogin.getId(),employeeLogin.getAccount(),"user",jwtProperties.getPrivateKey());
//        //临时的map
//        Map<String,Object> map=new HashMap<>();
//        //token
//        map.put("token",token);
//        //其他信息。。。和前端商量，比如name属性
//        map.put("nickName",employeeLogin.getName());
//
//        return new ResultDto(20000,"登录成功",map);
    }


}
